<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { artDelArticleService, artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import { ElMessageBox } from 'element-plus'

// 进去页面之前怎么样停止发送请求
const articleList = ref([]) // 文章列表
const total = ref(0) // 总条数

const articleEditRef = ref() // 抽屉表单绑定
// 查询参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_name: '',
  state: ''
})

const loading = ref(false)

const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  // 写错了 注意数据的位置 报错的位置在上面
  total.value = res.data.total
  loading.value = false
}

getArticleList()

// 分页设置
const onSizeChange = (pagesize) => {
  params.value.pagenum = 1
  params.value.pagesize = pagesize
  getArticleList()
}

const onCurrentChange = (pagenum) => {
  params.value.pagenum = pagenum
  getArticleList()
}

const reset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const onSelect = () => {
  params.value.pagenum = 1 // 重置页码
  getArticleList()
}
// 添加文章
const onAddArticle = () => {
  articleEditRef.value.open({})
}

// 编辑文章
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}
// 删除文章
const onDelArticle = async (row) => {
  await ElMessageBox({
    type: 'warning',
    title: '温馨提示',
    message: '你确定要删除这条信息吗',
    confirmButtonTest: '确认',
    showCancelButton: true,
    cancelButtonText: '取消'
  }).then(async () => {
    await artDelArticleService(row.id)
    ElMessage.success('删除成功')
    getArticleList()
  })
}

const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <!-- 搜索框 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <!-- 这里分类框显示这么小是为什么  -->
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select
          placeholder="请选择"
          style="width: 200px"
          v-model="params.state"
        >
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSelect">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="序号" width="180" />
      <el-table-column label="标题" width="180">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="pub_date" label="发布日期">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column fixed="right" label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            @click="onDelArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉弹窗 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 4, 5, 6]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="justify-content: right"
    />
  </page-container>
</template>
